<template>
  <el-container class="home-container">
    <el-header>
      <div><span>电商管理系统</span></div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="handleToggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eef"
          router
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          >
          <el-submenu :index="'/' + item.path" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>

          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMenuListRequest } from '../api/request'
export default {
  name: 'Home',
  data() {
    return {
      menuList: [],
      isCollapse: false
    }
  },
  created() {
    getMenuListRequest().then(res => {
      if (res.meta.status !== 200) this.$message.error('获取菜单信息失败')
      this.menuList = res.data
    })
  },
  methods: {
    logout() {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    handleToggleCollapse() {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>

<style lang='less' scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373d41;
  &>div {
    display: flex;
    align-items: center;
    span {
      font-size: 20px;
      color: #fff;
    }
  }
}
.el-aside {
  background-color: #333744;
  transition: all .3s;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  // padding: 10px 0;
  color: #fff;
  letter-spacing: .2em;
  background-color: #4a5064;
  cursor: pointer;
}
</style>